package org.gcb.core.sandbox;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.gcb.core.model.Armor;
import org.gcb.core.model.Item;
import org.gcb.core.model.Toon;
import org.gcb.core.model.Weapon;
import org.openide.explorer.view.IconView;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;

/**
 *
 * @author Jason
 */
public class JSlotDragSource extends DragSource{
}

class JSlotDragGestureRecognizer extends DragGestureRecognizer {

    public JSlotDragGestureRecognizer(DragSource ds) {
	super(ds);
    }

    public JSlotDragGestureRecognizer(DragSource ds, Component c) {
	super(ds, c);
    }

    public JSlotDragGestureRecognizer(DragSource ds, Component c, int sa) {
	super(ds, c, sa);
    }

    public JSlotDragGestureRecognizer(DragSource ds, Component c, int sa, DragGestureListener dgl) {
	super(ds, c, sa, dgl);
    }

    @Override
    protected void registerListeners() {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void unregisterListeners() {
	throw new UnsupportedOperationException("Not supported yet.");
    }

}

class JSlotDragGestureListener implements DragGestureListener {

    @Override
    public void dragGestureRecognized(DragGestureEvent dge) {
	try {
	    Transferable transferable = ((JSlot) dge.getComponent()).getModel().getItem();//
	    Cursor dragCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
	    Image dragImage = ImageUtilities.icon2Image(((JSlot) dge.getComponent()).getModel().getItem().getIcon());
	    Point imageOffset = new Point(5, 5);
	    DragSourceListener dragSourceListener = new JSlotDragSourceListener();
	    dge.startDrag(dragCursor, dragImage, imageOffset, transferable, dragSourceListener);
	} catch (Exception e) {
	    Exceptions.printStackTrace(e);
	}
    }

}

class JSlotDragSourceListener implements DragSourceListener {

    @Override
    public void dragEnter(DragSourceDragEvent dsde) {
	System.out.println(dsde);
    }

    @Override
    public void dragOver(DragSourceDragEvent dsde) {
	System.out.println(dsde);
    }

    @Override
    public void dropActionChanged(DragSourceDragEvent dsde) {
	System.out.println(dsde);
    }

    @Override
    public void dragExit(DragSourceEvent dse) {
	System.out.println(dse);
    }

    @Override
    public void dragDropEnd(DragSourceDropEvent dsde) {
	System.out.println(dsde);
    }

}

class BackpackItemDropTargetListener extends DropTargetAdapter {
    private DropTarget dropTarget;
    private IconView iconView;
    private Toon toon;

    public BackpackItemDropTargetListener(IconView iconView, Toon toon) {
	this.iconView = iconView;
	this.toon = toon;

	dropTarget = new DropTarget(iconView, DnDConstants.ACTION_MOVE, this, true);
    }

    @Override
    public void drop(DropTargetDropEvent dtde) {
	Transferable tr = dtde.getTransferable();
	try {
	    Item item = Item.class.cast(tr.getTransferData(Item.ITEM_DATA_FLAVOR));
	    if (dtde.isDataFlavorSupported(Item.ITEM_DATA_FLAVOR)) {
		dtde.acceptDrop(DnDConstants.ACTION_MOVE);
		this.toon.getEquipment().getCarried().add(item);
		return;
	    }
	    dtde.rejectDrop();
	} catch (UnsupportedFlavorException ex) {
	    Exceptions.printStackTrace(ex);
	} catch (IOException ex) {
	    Exceptions.printStackTrace(ex);
	}
	dtde.rejectDrop();
    }

}